package com.gis.servlet.lost;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSONArray;
import com.gis.bean.Point;
import com.gis.util.GisUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.stream.Collectors;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gis.dao.LostDao;
import com.gis.dto.LostDto;
import com.gis.util.PageResult;
import com.gis.util.ResponseUtil;

import cn.hutool.core.util.StrUtil;
import javax.servlet.http.HttpSession;


/**
 * 分页查询寻物启事
 * @author Administrator
 *
 */
@WebServlet("/lost/page")
public class LostPageServlet extends HttpServlet {
	LostDao lostDao = LostDao.getInstance();

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Map<String, Object> params = new HashMap<>();
		String username = "";
		if(req.getParameter("username") != null) {
			username = req.getParameter("username");
			params.put("username", username);
		} else {
			HttpSession session = req.getSession();
			Object user = session.getAttribute("username");
			if (user == null) {
				ResponseUtil.writePage(resp, null);
				return;
			} else {
				username = (String) user;
				params.put("username", username);
			}
		}
		// easyUI分页时 默认使用的参数是 page/rows 来表示页码和行数 而返回值也必须使用total/rows 其中total表示总行数 而rows表示返回集合数组
		String pageSize = req.getParameter("rows");
		if(pageSize == null) {
			pageSize = "5";
		}
		String pageNum = req.getParameter("page");
		if(pageNum == null) {
			pageNum = "1";
		}
		
		String goodsTypeId = req.getParameter("goodsTypeId");
		if (StrUtil.isNotBlank(goodsTypeId)) {
			params.put("goodsTypeId", goodsTypeId);
		}
		String startDate = req.getParameter("startDate");
		if (StrUtil.isNotBlank(startDate)) {
			params.put("startDate", startDate);
		}
		String endDate = req.getParameter("endDate");
		if(StrUtil.isNotBlank(endDate)) {
			params.put("endDate", endDate);
		}
		String points = req.getParameter("points");
		List<Point> pointList = new ArrayList<>();
		List<Double> lonList = new ArrayList<>();
		List<Double> latList = new ArrayList<>();
		if(StrUtil.isNotBlank(points)) {
			pointList = JSONArray.parseArray(points, Point.class);
		}
		for (Point point : pointList) {
			lonList.add(point.getLng());
			latList.add(point.getLat());
		}
		Double[] lons = Convert.toDoubleArray(lonList);
		Double[] lats = Convert.toDoubleArray(latList);

		Integer size = Integer.parseInt(pageSize);
		Integer num = Integer.parseInt(pageNum);
		params.put("offset", (num - 1) * size);
		params.put("rows", size);
		PageResult<LostDto> pageResult = lostDao.page(params);
		// 再按照前端选取的经纬度过滤
		if(CollectionUtil.isNotEmpty(pointList)) {
			List<LostDto> collect = pageResult.getRows().stream().filter(lostDto -> {
				// 保证返回的经纬度位于选取的点中
				Double lng = lostDto.getLng();
				Double lat = lostDto.getLat();

				boolean b = GisUtil.isInPolygon(lng, lat, lons, lats);
				return b;
			}).collect(Collectors.toList());
			pageResult.setRows(collect);
			pageResult.setTotal((long) collect.size());
		}
		
		ResponseUtil.writePage(resp, pageResult);
	}
	
	
	
}
